/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.1f1
 *Date:           2019-01-28
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using System;
using System.Collections.Generic;

namespace IFramework
{
	public class AStarMap3X : IAstarMap<AStarNode3X>
    {
        private List<AStarNode3X> neighborNodes;
        private AStarNode3X[,,] map;
        private int mapWith, mapHeight,mapLength;
        public AStarMap3X(AStarNode3X[,,] map, int mapWith,int mapHeight,int mapLength)
        {
            this.map = map;
            this.mapHeight = mapHeight;
            this.mapWith = mapWith;
            this.mapLength = mapLength;
            neighborNodes = new List<AStarNode3X>(27-1);
        }
        public float GetHCost(AStarNode3X start, AStarNode3X end)
        {
            return (float)Math.Sqrt((start.MapPosX - end.MapPosX) * (start.MapPosX - end.MapPosX) + (start.MapPosY - end.MapPosY) * (start.MapPosY - end.MapPosY)+ (start.MapPosZ - end.MapPosZ)* (start.MapPosZ - end.MapPosZ));
        }

        public AStarNode3X[] GetNeighborNodes(AStarNode3X node)
        {
            neighborNodes.Clear();
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    for (int k = 0; k < 3; k++)
                    {

                        if (node.MapPosX - 1 + i >= 0 && node.MapPosX - 1 + i < mapWith)
                        {
                            if (node.MapPosY - 1 + j >= 0 && node.MapPosY - 1 + j < mapHeight)
                            {
                                if (node.MapPosZ - 1 + k >= 0 && node.MapPosZ - 1 + k < mapLength)
                                {
                                    if (node.MapPosY - 1 + j == node.MapPosY && node.MapPosX - 1 + i == node.MapPosX && node.MapPosZ - 1 + k == node.MapPosZ) continue;
                                    if (map[node.MapPosX - 1 + i, node.MapPosY - 1 + j, node.MapPosZ - 1 + k].NodeType == AStarNodeType.Walkable)
                                    {
                                        neighborNodes.Add(map[node.MapPosX - 1 + i, node.MapPosY - 1 + j, node.MapPosZ - 1 + k]);
                                    }
                                }
                            }
                        }
                    }
                   
                }
            }
            return neighborNodes.ToArray();
        }

        public void Reset()
        {
            for (int i = 0; i < mapHeight; i++)
            {
                for (int j = 0; j < mapWith; j++)
                {
                    for (int k = 0; k < mapLength; k++)
                    {
                        map[i, j, k].Reset();
                    }
                }
            }
        }
    }
}
